var params = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;

var current = {};
var option = {};

$(function(){
	loadGrid();
	$('.check').on('click',search);
	//新增
	$('.add').on('click',addScene);
});

function search(){
	var keywords = $('#keywords').val();
	if(keywords!='')
	{
		params.materielName = keywords;
		loadGrid();
	}
}

function addScene(){
	$('.main_panel').load(webUrl+"public/page/materiel/addMateriel.html");
}

function loadGrid(){
	params.currentPage = currentPage;
	params.pageSize = pageSize;
	$.ajax({
		url: webUrl+'chwMateriel/findchwMateriel.do',
		type:'post',
		data:params
	}).done(function(data){
		var result = data.bo;
		var pager = {};
		$('#vendorGrid').datagrid({
			fitColumns:true,
			singleSelect:true,
			pagination:true,
			remoteSort:false,
			multiSort:true,
			loadMsg:"数据加载中，请稍后……",
			data:result.list,
			columns:[[
			          /*{field:'ck',checkbox:true},*/
			          /*{field:'id',title:'id',sortable:true},*/
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'segment1',title:'物料编码',align:'center',width:150,sortable:true},
			          {field:'materielName',title:'物料名称',align:'center',width:150,sortable:true},
			          {field:'vendorName',title:'供应商名称',align:'center',width:150,sortable:true},
			          {field:'materielTypeName',title:'物料类别名称',align:'center',width:150,sortable:true},
			          {field:'materielVersionName',title:'版本名称',align:'center',width:150,sortable:true},
			          {field:'materielUnitPrice',title:'采购单价',align:'center',width:150,sortable:true},
			          {field:'_operate',title:'操作',align:'center',width:60,formatter:formatOper},
			      ]],
			      onSortColumn: function (sort, order) {
			    	  setPager(pager,result.total);
         }
		});
		var pager = $('#vendorGrid').datagrid().datagrid('getPager');	// get the pager of datagrid
		setPager(pager,result.total);
		
		$('.delicon').on('click',deleteGuide);
		$('.dealicon').on('click',edit);
	});
}


var formatId = function(val, row,index){
	return (index+1)+(currentPage-1)*pageSize;
}

function setPager(pager,total){
	pager.pagination({
		total:total,
		pageNumber:currentPage,
	    pageSize:defaultPageSize,
	    layout:['first','prev','links','next','last'],
	    onSelectPage:function(cpage, psize){
	    	currentPage=cpage;
	    	pageSize=psize;
	    	loadGrid();
		}
	});
}

var formatOper = function(val,row,index){
	return '<a href="#"  class="dealicon  ml10" row-id="'+row.materielId+'" ></a> <a href="#;" class="delicon  ml10"  row-id="'+row.materielId+'" ></a>';
}

function edit(){
	var id = $(this).attr('row-id');
	localStorage.setItem("rowId", id);
	$('.main_panel').load(webUrl+"public/page/materiel/editMateriel.html");
}

var formatTime = function(val){
	if (val != null) {
		var date = new Date(val);
		return date.getFullYear() + '-' + (date.getMonth() + 1) + '-'+ date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
	}
}



function deleteGuide(){
	var id = $(this).attr('row-id');
	new Dialog({
		target:'#delbox',
		width:300,
		height:160,
		showYesBtn:true,
		showNoBtn:true,
		yesFn:function(){deleteSceneData(id);},
		noFn:function(){}
		}).show();
}

function deleteSceneData(id){
	$.ajax({
		url:webUrl+'chwMateriel/delChwMateriel.do',
		type:'post',
		data:{id:id}
	}).done(function(data){
		loadGrid();
	});
}